package com.clayecloud.module_house.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.clayecloud.module_house.R;
import com.clayecloud.module_house.databinding.FragmentHouseItemBinding;
import com.clayecloud.module_house.entity.House;

import java.util.List;

import io.reactivex.annotations.NonNull;

public class ListViewDatabindingAdapter extends BaseAdapter {

    private List<House> list;
    private Context context;


    public ListViewDatabindingAdapter(Context context, List<House> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public House getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Nullable
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        FragmentHouseItemBinding fragmentHouseItemBinding;
        if (convertView == null) {
            fragmentHouseItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.fragment_house_item, parent, false);
            convertView = fragmentHouseItemBinding.getRoot();
        } else {
            fragmentHouseItemBinding = DataBindingUtil.getBinding(convertView);
        }
        fragmentHouseItemBinding.setItem(getItem(position));
        return convertView;
    }
}
